package xjtu.sad.zbsj.user.login;

import xjtu.sad.zbsj.common.*;
import org.springframework.web.bind.annotation.*;
import xjtu.sad.zbsj.common.Result;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Random;

/**
 * @author lkm
 */
@RestController
@RequestMapping("/user/login")
public class LoginAPI {



    private static String codeId = "";
    private static String emailAddress = "";

    @Resource
    private LoginService loginService;

    @Resource
    private CommonService commonService;

    @PostMapping("/getRole")
    public Result getRole(@RequestBody LoginDto dto){
            String r_id = loginService.getRole2(dto);
            return Result.success("查询角色信息成功！",r_id);
    }

    @PostMapping("/getRole3")
    public Result getRole3(@RequestBody LoginDto dto){
        String r_id = loginService.getRole3(dto);
        return Result.success("查询角色信息成功！",r_id);
    }

    @PostMapping("")
    public Result login(@RequestBody LoginDto dto){

        boolean ok = loginService.checkLogin(dto);

        if(ok){
            //String t_id = commonService.addTime();
            String u_id = loginService.findUid(dto);
            dto.setU_id(u_id);
            //dto.setT_id(t_id);
//            loginService.addLogTime(dto);
            //生成一个登录令牌
            String token = TokenUtils.loginSign(dto.getU_id(),dto.getU_pwd());
            //user.setU_id(dto.getU_id());

            return Result.successData((String) token);//向客户端发送成功信号，并发送令牌//原来是object

        }

        return Result.fail(Result.ERR_CODE_BUSINESS, "fail");

    }

    @PostMapping("/email")
    public Result getUserIdAndPwd(@RequestBody LoginDto dto){

        User user = new User();
        String u_id = loginService.getUserId(dto.getU_eml());
        String u_pwd = loginService.getUserPwd(dto.getU_eml());

        System.out.println("不存在的用户的uid+++"+u_id);
        if(u_id!=null&&u_pwd!=null){//用户存在
            //SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
           // String t_id = commonService.addTime();
            //dto.setT_id(t_id);
            dto.setU_id(u_id);
//            loginService.addLogTime(dto);

            user.setU_id(u_id);
            user.setU_pwd(u_pwd);

            String token = TokenUtils.loginSign(user.getU_id(), user.getU_pwd());

            return Result.successData((String)token); // 向客户端发送成功信号，并发送令牌
        }
        else{
            return Result.fail(Result.ERR_CODE_BUSINESS, "用户不存在！");
        }

    }


    @RequestMapping("/getCode")
    @ResponseBody
    public Result getCode(String email , HttpServletRequest request){
        //随机生成一个验证码
        Integer code=new Random().nextInt(999999);
        //request.getSession().setAttribute("code",code.toString());
        //request.getSession().setAttribute("email",email);
        //String codeId = (String) request.getSession().getAttribute("code");
        codeId = code.toString();
        emailAddress = email;
        System.out.println("+++++++++++++"+code);

        new Thread(
                new Runnable() {
                    @Override
                    public void run() {
                        MailTool.send(email,code);
                    }
                }
        ).start();
        return Result.success("success");
    }

    //登陆
    @RequestMapping("/emailLogin")
    public Result emailLogin(String email , String code, HttpServletRequest request){
        //String emailAddress = (String) request.getSession().getAttribute("email");
        //String codeId = (String) request.getSession().getAttribute("code");
        System.out.println("+++++++++++++"+codeId);
        if (email.equals(emailAddress) && code.equals(codeId)){
            System.out.println("邮箱密码匹配成功");
            codeId="";
            return Result.success("success");
        }else {
            codeId="";
            return Result.fail(Result.ERR_CODE_BUSINESS, "验证码错误！");
        }
    }

}